package cn.taketoday.live.thread;

import org.apache.commons.lang3.StringUtils;

import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.file.LogFileTools;

/**
 * @author BanqiJane
 * @ClassName LogThread
 * @Description TODO
 * @date 2020年8月10日 下午12:30:30
 * @Copyright:2020 blogs.acproject.xyz Inc. All rights reserved.
 */
public class LogThread extends Thread {
  //	@SuppressWarnings("unused")
//	private Logger LOGGER = LogManager.getLogger(LogThread.class);
  public volatile boolean FLAG = false;

  @Override
  public void run() {
    // TODO 自动生成的方法存根
    String logString = null;
    super.run();
    while (!FLAG) {
      if (FLAG) {
        return;
      }
      if (GlobalData.webSocketProxy != null && !GlobalData.webSocketProxy.isOpen()) {
        return;
      }
      if (null != GlobalData.logString && !GlobalData.logString.isEmpty() && !StringUtils
              .isEmpty(GlobalData.logString.get(0))) {
        logString = GlobalData.logString.get(0);
        LogFileTools.getlogFileTools().logFile(logString);
        GlobalData.logString.remove(0);
      }
      else {
        synchronized(GlobalData.logThread) {
          try {
            GlobalData.logThread.wait();
          }
          catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
//						LOGGER.debug("日志线程关闭:" + e);
          }
        }
      }
      try {
        Thread.sleep(10);
      }
      catch (InterruptedException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
      }
    }
  }
}
